import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";

var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _class, _class2;

import React from 'react';
import { Popconfirm, Button, Table } from 'antd';
import { connect } from 'dva';
import { Bind } from 'lodash-decorators';
import { Header, Content } from 'components/Page';
import { enableRender, yesOrNoRender, operatorRender } from 'utils/renderer';
import notification from 'utils/notification';
import { tableScrollWidth } from 'utils/utils';
import intl from 'utils/intl';
import ColumnDrawer from "./ColumnDrawer";
import FilterForm from "./FilterForm";
var ColumnsTable = (_dec = connect(function (_ref) {
  var template = _ref.template,
      loading = _ref.loading;
  return {
    template: template,
    fetchListLoading: loading.effects['template/fetchColumnList'],
    detailLoading: loading.effects['template/fetchColumnDetail'],
    updateLoading: loading.effects['template/updateColumn'],
    createLoading: loading.effects['template/createColumn']
  };
}), _dec2 = Bind(), _dec3 = Bind(), _dec4 = Bind(), _dec5 = Bind(), _dec6 = Bind(), _dec7 = Bind(), _dec8 = Bind(), _dec9 = Bind(), _dec(_class = (_class2 = /*#__PURE__*/function (_React$Component) {
  _inherits(ColumnsTable, _React$Component);

  var _super = _createSuper(ColumnsTable);

  function ColumnsTable(props) {
    var _this;

    _classCallCheck(this, ColumnsTable);

    _this = _super.call(this, props);
    _this.state = {
      modalVisible: false
    };
    return _this;
  }

  _createClass(ColumnsTable, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.fetchColumnList();
      this.fetchColumnTypeCode();
    }
    /**
     * fetchTemplateColumnTypeCode - 查询层级<CIMP.TEMPLATE.COLUMNTYPE>code
     * @return {Array}
     */

  }, {
    key: "fetchColumnTypeCode",
    value: function fetchColumnTypeCode() {
      var dispatch = this.props.dispatch;
      return dispatch({
        type: 'template/queryCode',
        payload: {
          lovCode: 'CIMP.TEMPLATE.COLUMNTYPE'
        }
      });
    }
  }, {
    key: "fetchColumnList",
    value: function fetchColumnList() {
      var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var _this$props = this.props,
          dispatch = _this$props.dispatch,
          match = _this$props.match,
          _this$props$template$ = _this$props.template.columnPagination,
          columnPagination = _this$props$template$ === void 0 ? {} : _this$props$template$;
      var targetId = match.params.sheetId;
      dispatch({
        type: 'template/fetchColumnList',
        payload: _objectSpread({
          page: columnPagination,
          targetId: targetId
        }, params)
      });
    }
    /**
     * handlePagination - 分页设置
     * @param {object} pagination - 分页对象
     */

  }, {
    key: "handlePagination",
    value: function handlePagination(pagination) {
      this.fetchColumnList({
        page: pagination
      });
    }
  }, {
    key: "showModal",
    value: function showModal(record, flag) {
      var dispatch = this.props.dispatch;

      if (flag) {
        dispatch({
          type: 'template/fetchColumnDetail',
          payload: record
        });
      } else {
        dispatch({
          type: 'template/updateState',
          payload: {
            templateColumnDetail: {}
          }
        });
      }

      this.setState({
        modalVisible: true
      });
    }
  }, {
    key: "hideModal",
    value: function hideModal() {
      this.setState({
        modalVisible: false
      });
    }
  }, {
    key: "handleDeleteColumn",
    value: function handleDeleteColumn(record) {
      var _this2 = this;

      var dispatch = this.props.dispatch;
      dispatch({
        type: 'template/deleteColumn',
        payload: record
      }).then(function (res) {
        if (res) {
          notification.success();

          _this2.fetchColumnList();
        }
      });
    }
    /**
     * 查询表单
     * @param {object} params - 查询参数
     */

  }, {
    key: "handleSearch",
    value: function handleSearch(params) {
      this.fetchColumnList(_objectSpread(_objectSpread({}, params), {}, {
        page: {}
      }));
    }
  }, {
    key: "handleSaveColumn",
    value: function handleSaveColumn(params) {
      var _this3 = this;

      var _this$props2 = this.props,
          dispatch = _this$props2.dispatch,
          match = _this$props2.match,
          _this$props2$template = _this$props2.template.templateColumnDetail,
          templateColumnDetail = _this$props2$template === void 0 ? {} : _this$props2$template;
      var sheetId = match.params.sheetId;
      var columnId = templateColumnDetail.id;
      dispatch({
        type: "template/".concat(columnId !== undefined ? 'updateColumn' : 'createColumn'),
        payload: _objectSpread(_objectSpread({
          targetId: sheetId,
          changeDataFlag: 0
        }, templateColumnDetail), params)
      }).then(function (res) {
        if (res) {
          notification.success();

          _this3.hideModal();

          _this3.fetchColumnList();
        }
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this4 = this;

      var _this$props3 = this.props,
          match = _this$props3.match,
          _this$props3$fetchLis = _this$props3.fetchListLoading,
          fetchListLoading = _this$props3$fetchLis === void 0 ? false : _this$props3$fetchLis,
          _this$props3$detailLo = _this$props3.detailLoading,
          detailLoading = _this$props3$detailLo === void 0 ? false : _this$props3$detailLo,
          _this$props3$updateLo = _this$props3.updateLoading,
          updateLoading = _this$props3$updateLo === void 0 ? false : _this$props3$updateLo,
          _this$props3$createLo = _this$props3.createLoading,
          createLoading = _this$props3$createLo === void 0 ? false : _this$props3$createLo,
          _this$props3$template = _this$props3.template,
          _this$props3$template2 = _this$props3$template.code,
          code = _this$props3$template2 === void 0 ? {} : _this$props3$template2,
          _this$props3$template3 = _this$props3$template.templateColumnList,
          templateColumnList = _this$props3$template3 === void 0 ? [] : _this$props3$template3,
          _this$props3$template4 = _this$props3$template.templateColumnDetail,
          templateColumnDetail = _this$props3$template4 === void 0 ? {} : _this$props3$template4,
          _this$props3$template5 = _this$props3$template.columnPagination,
          columnPagination = _this$props3$template5 === void 0 ? {} : _this$props3$template5,
          templateType = _this$props3.match.params.templateType;
      var modalVisible = this.state.modalVisible;
      var id = match.params.id;
      var columns = [{
        title: intl.get("cimp.template.model.template.columnIndex").d('列号'),
        width: 80,
        dataIndex: 'columnIndex'
      }, {
        title: intl.get("cimp.template.model.template.columnCode").d('列编码'),
        dataIndex: 'columnCode'
      }, {
        title: intl.get("cimp.template.model.template.columnName").d('列名'),
        dataIndex: 'columnName'
      }, {
        title: intl.get("cimp.template.model.template.columnType").d('列类型'),
        width: 100,
        dataIndex: 'columnTypeMeaning'
      }, {
        title: intl.get("cimp.template.model.template.formatMask").d('格式掩码'),
        width: 100,
        dataIndex: 'formatMask'
      }, {
        title: intl.get("cimp.template.model.template.length").d('长度'),
        width: 100,
        dataIndex: 'length'
      }, {
        title: intl.get("cimp.template.model.template.maxValue").d('最大值'),
        width: 100,
        dataIndex: 'maxValue'
      }, {
        title: intl.get("cimp.template.model.template.minValue").d('最小值'),
        width: 100,
        dataIndex: 'minValue'
      }, {
        title: intl.get("cimp.template.model.template.validateSet").d('验证值集'),
        width: 100,
        dataIndex: 'validateSet'
      }, {
        title: intl.get("cimp.template.model.template.regularExpression").d('正则式'),
        width: 100,
        dataIndex: 'regularExpression'
      }, {
        title: intl.get("cimp.template.model.template.nullable").d('允许为空'),
        width: 100,
        dataIndex: 'nullableFlag',
        render: yesOrNoRender
      }, {
        title: intl.get("cimp.template.model.template.validate").d('数据验证'),
        width: 100,
        dataIndex: 'validateFlag',
        render: yesOrNoRender
      }, {
        title: intl.get("cpaas.common.status").d('状态'),
        dataIndex: 'enabledFlag',
        width: 100,
        render: enableRender
      }, {
        title: intl.get('cpaas.common.button.action').d('操作'),
        dataIndex: 'operator',
        fixed: 'right',
        width: 110,
        render: function render(val, record) {
          var operators = [{
            key: 'edit',
            ele: /*#__PURE__*/React.createElement("a", {
              onClick: function onClick() {
                _this4.showModal(record, true);
              }
            }, intl.get('cpaas.common.button.edit').d('编辑')),
            len: 2,
            title: intl.get('cpaas.common.button.edit').d('编辑')
          }, {
            key: 'delete',
            ele: /*#__PURE__*/React.createElement(Popconfirm, {
              title: intl.get("cimp.template.view.message.title.confirmDelete").d('确定删除？'),
              onConfirm: function onConfirm() {
                return _this4.handleDeleteColumn(record);
              },
              style: {
                textAlign: 'center'
              }
            }, /*#__PURE__*/React.createElement("a", null, intl.get('cpaas.common.button.delete').d('删除'))),
            len: 2,
            title: intl.get('cpaas.common.button.delete').d('删除')
          }];
          return operatorRender(operators, record);
        }
      }];
      return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Header, {
        title: intl.get('cimp.template.view.message.title.templateCol').d('模板列'),
        backPath: "/cimp/template/detail/".concat(id)
      }, /*#__PURE__*/React.createElement(Button, {
        onClick: function onClick() {
          return _this4.showModal({}, false);
        },
        type: "primary",
        icon: "plus"
      }, intl.get('cpaas.common.button.create').d('新建'))), /*#__PURE__*/React.createElement(Content, null, /*#__PURE__*/React.createElement("div", {
        className: "table-list-search"
      }, /*#__PURE__*/React.createElement(FilterForm, {
        onSearch: this.handleSearch
      })), /*#__PURE__*/React.createElement(Table, {
        bordered: true,
        rowKey: "id",
        columns: columns,
        scroll: {
          x: tableScrollWidth(columns)
        },
        loading: fetchListLoading,
        dataSource: templateColumnList,
        pagination: columnPagination,
        onChange: this.handlePagination
      }), /*#__PURE__*/React.createElement(ColumnDrawer, {
        title: templateColumnDetail.id !== undefined ? intl.get('cimp.template.view.message.title.editCol').d('编辑列') : intl.get('cimp.template.view.message.title.createCol').d('新建列'),
        initLoading: detailLoading,
        loading: templateColumnDetail.id !== undefined ? updateLoading : createLoading,
        modalVisible: modalVisible,
        initData: templateColumnDetail,
        columnTypeCode: code['CIMP.TEMPLATE.COLUMNTYPE'],
        onCancel: this.hideModal,
        onOk: this.handleSaveColumn,
        templateType: templateType
      })));
    }
  }]);

  return ColumnsTable;
}(React.Component), (_applyDecoratedDescriptor(_class2.prototype, "fetchColumnTypeCode", [_dec2], Object.getOwnPropertyDescriptor(_class2.prototype, "fetchColumnTypeCode"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "fetchColumnList", [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, "fetchColumnList"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handlePagination", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "handlePagination"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "showModal", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "showModal"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "hideModal", [_dec6], Object.getOwnPropertyDescriptor(_class2.prototype, "hideModal"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleDeleteColumn", [_dec7], Object.getOwnPropertyDescriptor(_class2.prototype, "handleDeleteColumn"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleSearch", [_dec8], Object.getOwnPropertyDescriptor(_class2.prototype, "handleSearch"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleSaveColumn", [_dec9], Object.getOwnPropertyDescriptor(_class2.prototype, "handleSaveColumn"), _class2.prototype)), _class2)) || _class);
export { ColumnsTable as default };